<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="space-between">
        <el-col>
          <span>{{ treeNode.manager }} </span>
        </el-col>
        <el-col>
          <el-dropdown trigger="click" @command="operateDepts">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >编辑部门
              </el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="del"
              >删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { deleteDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object, // 对象类型
      required: true // 要求对方使用您的组件的时候 必须传treeNode属性 如果不传 就会报错
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepts(type) {
      switch (type) {
        case 'add':
          this.$emit('addDepts', this.treeNode)
          break
        case 'edit':
          console.log('edit')
          this.$emit('editDepts', this.treeNode)

          break
        default:
          this.$confirm('您确定要删除吗？', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          })
            .then(() => {
              return deleteDepartments(this.treeNode.id)
            })
            .then(() => {
              // 这里自定义事件，然后去父组件板顶这个事件
              this.$emit('delDepts')
              this.$message.success('删除成功')
            })
          break
      }
    }
  }
}
</script>

<style></style>
